package org.esti.service.impl;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import org.esti.model.Appraisal;
import org.esti.model.Level;
import org.esti.model.dao.LevelDAO;
import org.esti.service.AppraisalService;
import org.esti.service.LevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Artem Loginov (logart)
 */

/**
 * {@inheritDoc}
 */
@Service
public class LevelServiceImpl implements LevelService {


    @Autowired
    private LevelDAO dao;

    @Autowired
    private AppraisalService apprailsalService;

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<Level> getAllLevels() {
        return (Collection<Level>) Collections2.filter(dao.getAllLevels(), new Predicate<Level>() {
            public boolean apply(Level level) {
                return (level.getName() != null);
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Level getLevel(Long id) {
        return dao.get(id);
    }

    @Override
    public void add(Level level) {
        dao.create(level);
    }

    @Override
    public void remove(long id) {
        dao.remove(id);
    }

    @Override
    public Map<Long, String> getAllLevelsMap() {
        Collection<Level> levels = getAllLevels();
        Map<Long, String> levelsMap = new HashMap<Long, String>();
        for (Level level : levels) {
            levelsMap.put(level.getId(), level.getName());
        }
        return levelsMap;
    }

    @Override
    public Set<Level> getAvailableLevels() {
        Set<Level> levels = new HashSet<Level>();
        List<Appraisal> appraisals = apprailsalService.getAvailableAppraisals();
        for (Appraisal appraisal : appraisals) {
            levels.add(appraisal.getLevel());
        }
        return levels;
    }
}
